﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using CrawlerCore;
using HtmlAgilityPack;

namespace WCrawlerLib
{
    public class BrowserSession_vBulletin3x : BrowserSession_vBulletin
    {
        public BrowserSession_vBulletin3x()
        {
            ForumTypeId = enumForumType.vBulletin3x;
        }

        public override List<DataRow> SearchThreadsByPaging(string forumUrl, HtmlDocument doc, DataTable table)
        {
            List<DataRow> rowList = new List<DataRow>();

            string xPath = @".//tr[td[contains(@id,'td_threadtitle')]]";
            HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes(xPath);
            if (nodeCollection != null)
            {
                foreach (HtmlNode node in nodeCollection)
                {
                    DataRow row = table.NewRow();
                    xPath = ".//a[contains(@id,'thread_title')]";
                    HtmlNodeCollection node1 = node.SelectNodes(xPath);

                    //1. Get thread num
                    //Regex r = new Regex("[0-9]+");
                    //Match m = r.Match(node1[0].Attributes["id"].Value);
                    //row["ThreadNum"] = m.Value;

                    //2. Get title hyperlink
                    string href = node1[0].Attributes["href"].Value;
                    //row["Topic"] = node1[0].OuterHtml.Replace(href, forumUrl + "/" + href);
                    row["Title"] = node1[0].InnerHtml;
                    row["Url"] = GeneralClass.GetFullUrl(forumUrl, href);

                    //3. Get Replies and Views title="Replies: 4, Views: 12"
                    xPath = ".//td[contains(@title, 'Replies') and contains(@title, 'Views')]";
                    node1 = node.SelectNodes(xPath);
                    Regex r = new Regex("[0-9]+");
                    MatchCollection ms = r.Matches(node1[0].Attributes["title"].Value);
                    row["Replies"] = ms[0].Value;
                    row["Views"] = ms[1].Value;

                    //4. Get Forum
                    xPath = ".//td[7]/a";
                    node1 = node.SelectNodes(xPath);
                    href = node1[0].Attributes["href"].Value;
                    row["Forum"] = node1[0].OuterHtml.Replace(href, forumUrl + "/" + href);

                    //Get post num
                    //row["PostNum"] = GetPostNum(row["Url"].ToString());

                    rowList.Add(row);
                }
            }
            return rowList;
        }

        /// <summary>
        /// Get PostId
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public override string[] GetThreadPostNum(HtmlDocument doc)
        {
            string[] result = new string[2];

            if (doc != null)
            {
                //Find thread num
                //newreply.php?do=postreply&t=3502709
                string xPath = ".//form[contains(@action,'newreply')]";
                HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes(xPath);
                if (nodeCollection != null)
                {
                    string replyUrl = nodeCollection[0].Attributes["action"].Value.Replace("postcount", "");
                    Regex r = new Regex(@"newreply\.php\?do=postreply(\&|\&amp\;)t=(?<threadnum>[0-9]+)");
                    Match m = r.Match(replyUrl);
                    if (m.Success)
                    {
                        result[0] = m.Result("${threadnum}");
                    }
                }

                //Find first post num
                xPath = ".//a[starts-with(@id,'postcount') and substring-after(@id, 'postcount') >= 0]";
                nodeCollection = doc.DocumentNode.SelectNodes(xPath);
                if (nodeCollection != null)
                    result[1] = nodeCollection[0].Attributes["id"].Value.Replace("postcount", "");
            }
            return result;
        }

        public override bool PostReply(string topicUrl, string forumUrl, string message)
        {
            throw new NotImplementedException();
        }
    }
}